package com.qubi.exchange.biz;

import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import com.qubi.exchange.entity.AreaName;
import com.qubi.exchange.mapper.AreaNameMapper;
import com.qubi.exchange.mapper.CoinlMapper;
import com.qubi.exchange.mapper.ColaCoinSymbolMapper;
import com.qubi.me.entity.ColaCoin;
import com.qubi.me.entity.ColaCoinSymbol;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/*
 * @author:wx
 * @description:
 * @create:2018-08-28  20:07
 */
@Service
public class ColaSymbolBiz {
    @Autowired
    ColaCoinSymbolMapper colaCoinSymbolMapper;

    @Autowired
    AreaNameMapper areaNameMapper;

    @Autowired
    CoinlMapper coinlMapper;


    @Cached(cacheType = CacheType.LOCAL, expire = 40)

    public   List<String>getAllSymbol()
    {
        List<String>symbolCodes=new ArrayList<>();

        List<ColaCoinSymbol>colaCoinSymbols= colaCoinSymbolMapper.selectAll();
        for(Object colaCoinSymbol:colaCoinSymbols){
             Class clz=   colaCoinSymbol.getClass();
             Class clz2=ColaCoinSymbol.class;

             boolean isSame=colaCoinSymbol instanceof ColaCoinSymbol;

            if(colaCoinSymbol instanceof ColaCoinSymbol) {



                ColaCoinSymbol c=(ColaCoinSymbol)colaCoinSymbol;
                String code = c.getCoinCode() + "_" + c.getSymbol();
                symbolCodes.add(code);
            }
        }
        return symbolCodes;
    }
    public List<ColaCoinSymbol> getCoinSymbolBySymbol(String symbol) {
        List<ColaCoinSymbol> list;
        Example example=new Example(ColaCoinSymbol.class);
        if (!StringUtils.isEmpty(symbol)) {
            example.createCriteria().andEqualTo("symbol",symbol);
        }
        list=colaCoinSymbolMapper.selectAll();
        list.forEach(x->{
        });
        return list;
    }


    @Cached(cacheType = CacheType.LOCAL,expire = 60)
    public ColaCoinSymbol getPair(String pair){
        String codes[]=pair.split("_");
        ColaCoinSymbol symbols=new ColaCoinSymbol();
        symbols.setCoinCode(codes[0]);
        symbols.setSymbol(codes[1]);
        return colaCoinSymbolMapper.selectOne(symbols);
    }

    @Cached(cacheType = CacheType.LOCAL,expire = 60)

    public List<String>getALLCoin()
    {
        return coinlMapper.selectAll().stream().map(ColaCoin::getCoinCode).collect(Collectors.toList());
    }


    @Cached(cacheType = CacheType.LOCAL, expire = 60)
    public List<String> getSymbols() {
        var list = areaNameMapper.selectAll();
        var reslut = list.stream().filter(x -> x.getType() == 0).sorted(Comparator.comparingInt(AreaName::getSort)).map(AreaName::getName).collect(Collectors.toList());
        return reslut;
    }
    @Cached(cacheType = CacheType.LOCAL, expire = 60)
    public List<String> getIndexSymbols() {
        var list = areaNameMapper.selectAll();
        var reslut = list.stream().filter(x -> x.getType() == 1).sorted(Comparator.comparingInt(AreaName::getSort)).map(AreaName::getName).collect(Collectors.toList());
        return reslut;
    }
    @Cached(cacheType = CacheType.LOCAL, expire = 60)

    public String getCoinDescr(String coinCode)
    {
        Example example=new Example(ColaCoin.class);
        example.createCriteria().andEqualTo("coinCode",coinCode);
        var coin= coinlMapper.selectOneByExample(example);
        return coin.getDescr();
    }
}
